//
//  XPJsonToModelUtil.swift
//  PPKit
//
//  Created by Mr.Gao on 2021/6/18.
//  Copyright © 2021 scoresdata.net. All rights reserved.
//

import HandyJSON

class XPJsonToModelUtil {
    
    /// 将json文件，转换为数组模型
    class func model_by_filejson_Arr<T>(_ filePath:String, _ type:T.Type) -> [T] where T : HandyJSON {
        
        let strPath = Bundle.main.path(forResource: filePath, ofType: "geojson") ?? ""
        var dataJson: String? = nil
        do {
            dataJson = try String(contentsOfFile: strPath, encoding: .utf8)
        } catch {
        }
        if let models = [T].deserialize(from: dataJson) as? [T] {
            return models
        }else{
            assert(false, "⚠️沃日，JSON错了")
        }
        return [T]()
    }

    /// 将json文件，转换为字典模型
    class func model_by_filejson_dic<T>(_ filePath:String, _ type:T.Type) -> T where T :HandyJSON {
        let strPath = Bundle.main.path(forResource: filePath, ofType: "geojson") ?? ""
        var dataJson: String? = nil
        do {
            dataJson = try String(contentsOfFile: strPath, encoding: .utf8)
        } catch {
        }
        if let model = T.deserialize(from: dataJson) {
            return model
        }else{
              assert(false, "⚠️沃日，JSON错了")
        }
        return T()
    }
}
